$(function () {
    //变量抽取
    var client_dataGrid = $("#client_dataGrid");
    var client_dialog = $("#client_dialog");
    var client_form = $("#client_form");
    var client_remarkForm = $("#client_remarkForm");
    var client_moveClient = $("#client_moveClient");
    var client_moveClientForm = $("#client_moveClientForm");
    var client_remark = $("#client_remark");

    client_dataGrid.datagrid({
        url: "/client/query.do",
        fitColumns: true,//列自适应
        fit: true,
        queryParams: {
            state: 0
        },
        align: 'center',
        sortName: 'hireDate',
        sortOrder: 'desc',
        columns: [[
            {field: 'createDate', title: '创建日期', width: 100, align: 'center', sortable: true},
            {
                field: 'createEmp', title: '创建人', width: 100
            },
            {
                field: 'chargeEmp', title: '负责人', width: 100
            },
            {
                field: 'clientName', title: '客户名称', width: 100
            },
            {field: 'age', title: '年龄', width: 100, align: 'center'},
            {
                field: 'gender', title: '性别', width: 100, formatter: function (value) {
                return value ? "男" : "女";
            }
            },
            {
                field: 'profession', title: '职业', width: 100, formatter: function (value) {
                return value ? value.name : "";
            }
            },
            {
                field: 'salaryLevel', title: '薪资水平', width: 100, formatter: function (value) {
                return value ? value.name : "";
            }
            },
            {field: 'tel', title: '电话', width: 100, align: 'center'},
            {field: 'email', title: '邮箱', width: 100, align: 'center'},
            {field: 'clientAddress', title: '客户地址', width: 100, align: 'center'},
            {
                field: 'clientSource', title: '客户来源', width: 100, align: 'center', formatter: function (value) {
                return value ? value.name : ""
            }
            },
            {field: 'qq', title: 'QQ', width: 100, align: 'center'},
            {field: 'idCard', title: '身份证', width: 100, align: 'center'}
        ]],
        toolbar: "#tb",//工具
        pagination: true,//分页
        rownumbers: true,//行号
        singleSelect: true//只允许选择一行
    });

    client_dialog.dialog({
        width: 270,
        height: 430,
        buttons: "#btns",
        closed: true,
        onClose: function () {
            client_form.form("clear")
        },
        resizeable:true
    });
    client_moveClient.dialog({
        width: 270,
        height: 260,
        closed: true,
        buttons: "#btns_moveClient",
        onClose: function () {
            client_moveClientForm.form("clear")
        },
        resizeable:true
    });
    client_remark.dialog({
        width: 270,
        height: 260,
        closed: true,
        buttons: "#btns_remark",
        onClose: function () {
            client_remarkForm.form("clear")
        },
        resizeable:true
    });
    //方法抽成对象统一管理
    var methodObj = {//添加按钮
        add: function () {
            client_dialog.dialog("open");
            client_dialog.dialog("setTitle", "添加");
        },
        //编辑按钮
        edit: function () {
            var row = client_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '选择需要编辑的数据', 'info');
            } else {
                row["profession.id"] = row.profession.id;
                row["clientSource.id"] = row.clientSource.id;
                row["salaryLevel.id"] = row.salaryLevel.id;
                client_form.form("load", row);
                client_dialog.dialog("setTitle", "编辑");
                client_dialog.dialog("open");
            }
        },
        reload: function () {
            client_dataGrid.datagrid("load");
        },
        save: function () {
            client_form.form("submit", {
                url: "/client/saveOrUpdate.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data) {
                        $.messager.alert('提示', '保存成功', 'info', function () {
                            client_dialog.dialog("close");
                            client_dataGrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('提示', '保存失败', 'info');
                    }
                }
            })
        },
        //取消按钮
        cancel: function () {
            client_dialog.dialog("close")
        },
        //移入资源池
        moveResource: function () {
            var row = client_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '选择需要移入资源池的数据', 'info');
            } else {
                $.messager.confirm("提示", "是否要将 " + row.clientName + " 移入资源池", function (r) {
                    if (r) {
                        $.get("/client/moveResource.do", {clientId: row.id}, function (data) {
                            if (data.success) {
                                $.messager.alert('提示', '操作成功', 'info', function () {
                                    client_dataGrid.datagrid("reload");
                                })
                            } else {
                                $.messager.alert('提示', data.msg, 'error')
                            }
                        }, "json")
                    }
                })
            }
        },
        //移交客户
        moveClient: function () {
            var row = client_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '选择需要移交的数据', 'info');
            } else {
                client_moveClient.dialog("setTitle", "移交客户");
                client_moveClient.dialog("open");
                client_moveClientForm.form("load", row);
            }
        },
        saveMove: function () {
            client_moveClientForm.form("submit", {
                url: "/transferRecord/insertTransferRecord.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data) {
                        $.messager.alert('提示', '移交成功', 'info', function () {
                            client_moveClient.dialog("close");
                            client_dataGrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('提示', '移交失败', 'info');
                    }
                }
            })
        },
        cancelMove: function () {
            client_moveClient.dialog("close");
        },
        //标记为开发失败
        defeated: function () {
            var row = client_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '请选择一条的数据', 'info');
            } else {
                if(!row.chargeEmp){
                    $.messager.alert('提示',"还没有安排负责人你就知道开发失败了?","error");
                    return;
                }
                $.messager.confirm('提示', "是否要将 " + row.clientName + " 标记为开发失败?", function (r) {
                    if (r) {
                        client_remark.dialog("open");
                        client_remarkForm.form("load",row);
                        $("[name=state]").val(-1);
                    }
                });
            }
        },
        //标记为客户流失
        loss: function () {
            var row = client_dataGrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('提示', '请选择一条的数据', 'info');
            } else {
                if(!row.chargeEmp){
                    $.messager.alert('提示',"还没有安排负责人你就知道流失了?","error");
                    return;
                }
                $.messager.confirm('提示', "是否要将 " + row.clientName + " 标记为客户流失?", function (r) {
                    if (r) {

                        if (r) {
                            client_remark.dialog("open");
                            client_remarkForm.form("load",row);
                            $("[name=state]").val(-2);
                        }
                    }
                });
            }
            //获取到输入框的值,并提交查询条件
        },
        saveRemark:function (state) {
            var row = client_dataGrid.datagrid("getSelected");
            $.post("/lossRecord/insertLossRecord.do",client_remarkForm.serialize(), function (data) {
                if (data.success) {
                    $.messager.alert('提示', '操作成功', 'info', function () {
                        client_remark.dialog("close");
                        client_dataGrid.datagrid("reload");
                    })
                } else {
                    $.messager.alert('提示', data.msg, 'error')
                }
            }, "json")
        },
        cancelRemark:function () {
            client_remark.dialog("close");
        },

        //获取到输入框的值,并提交查询条件
        searchForm: function () {
            var beginTime = $("[name=beginTime]").val();
            var endTime = $("[name=endTime]").val();
            var idCard = $("[name=idCard]").val();
            var tel = $("[name=tel]").val();
            var chargeEmpName = $("[name=chargeEmpName]").val();
            var clientName = $("[name=clientName]").val();
            var gender = $(".gender_combobox").combobox('getValue');

            client_dataGrid.datagrid('load', {
                beginTime: beginTime,
                endTime: endTime,
                idCard: idCard,
                tel: tel,
                chargeEmpName: chargeEmpName,
                clientName: clientName,
                gender: gender,
                state: 0
            });
        },
        //清空查询框
        clearSearchForm: function () {
            $("#beginTime").textbox("setValue",'');
            $("#endTime").textbox("setValue",'');
            $("#idCard").textbox("setValue",'');
            $("#tel").textbox("setValue",'');
            $("#chargeEmpName").textbox("setValue",'');
            $("#clientName").textbox("setValue",'');
            $(".gender_combobox").combobox('setValue', '');
        }
    };

    //按钮统一绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });
});